home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / uk-sendmail2.1 / Support / authorise.c next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  3.1 KB  |  174 lines

  1. /*
  2.  *  authorise  -  sendmail authorisation program
  3.  *
  4.  *    Given a channel name, sender address and recipient address or
  5.  *    host, it matches these against entries in an authorisation
  6.  *    file to see if permission is granted to send the mail.
  7.  *
  8.  *    If successful, the mailer interface is called; else the 
  9.  *    appropriate error status is returned to sendmail.
  10.  *
  11.  *    Written by Jim Crammond.    <jac@ic.doc>    5/87
  12.  */
  13. #include <stdio.h>
  14. #include <sysexits.h>
  15.  
  16. #define AUTHFILE    "/usr/lib/authorisations"
  17.  
  18. #define    CHANSIZE    64
  19. #define    ADDRSIZE    256
  20. #define LINESIZE    1024
  21.  
  22. char    a_chan[CHANSIZE];
  23. char    a_from[ADDRSIZE];
  24. char    a_to[ADDRSIZE];
  25.  
  26. char    *authfile = AUTHFILE;
  27.  
  28. int    negative = 0;
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char    *argv[];
  33. {
  34.     char    *progname, *channel;
  35.     char    *from_addr, *to_addr;
  36.     FILE    *afp, *fopen();
  37.     char    line[LINESIZE];
  38.     int    cnt, nfields;
  39.     register char *p;
  40.     char    *index();
  41.     int    gotmatch = 0;
  42.  
  43.  
  44.     progname  = *argv++;
  45.  
  46.     if (argv[0][0] == '-' && argv[0][1] == 'f')
  47.     {    authfile = argv[1];
  48.         argc -= 2;
  49.         argv += 2;
  50.     }
  51.  
  52.     if (argc < 4)
  53.     {    printf("usage: %s [-f authfile] channel from to command [args]\n", progname);
  54.         exit(EX_USAGE);
  55.     }
  56.  
  57.     channel   = *argv++;
  58.     from_addr = *argv++;
  59.     to_addr   = *argv++;
  60.  
  61.     if ((afp = fopen(authfile, "r")) == NULL)
  62.     {    printf("warning: cannot open authorisation file\n");
  63.         gotmatch = 1;
  64.     }
  65.  
  66.     while (!gotmatch && fgets(line, sizeof(line), afp))
  67.     {    cnt++;
  68.         if ((p = index(line, '\n')) != NULL)
  69.             *p = '\0';
  70.         if ((p = index(line, '#')) != NULL)
  71.             *p = '\0';
  72.         
  73.         for (p=line; *p == ' ' && *p == '\t'; p++)
  74.             ;
  75.  
  76.         if (*p == '\0')
  77.             continue;
  78.  
  79.         nfields = sscanf(p, "%s %s %s", a_chan, a_from, a_to);
  80.  
  81.         if (nfields != 3)
  82.         {    printf("warning: line %d ignored: \"%s\"\n", cnt, line);
  83.             continue;
  84.         }
  85.  
  86.         negative = 0;
  87.         if (strcmp(channel, a_chan) == 0 &&
  88.             match(from_addr, a_from) && match(to_addr, a_to))
  89.         {    if (negative > 0)
  90.                 gotmatch = -1;
  91.             else
  92.                 gotmatch = 1;
  93.         }
  94.  
  95. #ifdef DEBUG
  96.         printf("%d: %s - %s\n", cnt, line,
  97.             gotmatch ? "matched" : "no match");
  98. #endif DEBUG
  99.     }
  100.  
  101.     if (gotmatch <= 0)
  102.     {    printf("%s: %s is not authorised to send to host/address %s\n",
  103.             progname, from_addr, to_addr);
  104.         exit(EX_NOPERM);
  105.     }
  106.  
  107.     execv(argv[0], argv);
  108.  
  109.     printf("%s: cannot exec %s\n", progname, argv[0]);
  110.     exit(EX_UNAVAILABLE);
  111. }
  112.  
  113.  
  114. /*
  115.  *  MATCH  --  match the strings s1 and s2.
  116.  *        s2 can contain wildcards and lists
  117.  */
  118. match(s1, s2)
  119. char    *s1, *s2;
  120. {
  121.     char    lbuf[ADDRSIZE];
  122.     char    *rest, *lp;
  123.     char    *index();
  124.  
  125.     if (*s2 == '\\')                    /*  escape  */
  126.     {    if (*s1 == *(s2+1) && match(s1+1, s2+2))
  127.             return(1);
  128.     }
  129.  
  130.     else if (*s2 == '*')                    /*  wildcard  */
  131.     {    if (match(s1, ++s2))
  132.             return(1);
  133.  
  134.         while (*s1++)
  135.         {    if (match(s1, s2))
  136.                 return(1);
  137.         }
  138.     }
  139.  
  140.     else if (*s2 == '{' && (rest = index(++s2, '}')))    /*  list  */
  141.     {    rest++;
  142.         lp = lbuf;
  143.  
  144.         while (s2 != rest)
  145.         {    while (*s2 && *s2 != ',' && *s2 != '}')
  146.                 *lp++ = *s2++;
  147.             strcpy(lp, rest);
  148.  
  149.             if (match(s1, lbuf))
  150.                 return(1);
  151.  
  152.             lp = lbuf;
  153.             s2++;
  154.         }
  155.     }
  156.  
  157.     else if (*s2 == '^')                 /*  negative match  */
  158.     {    if (match(s1, ++s2))
  159.         {    negative++;
  160.             return(1);
  161.         }
  162.     }
  163.  
  164.     else if (*s1 == *s2)                    /*  literal  */
  165.     {    if (*s1 == '\0')
  166.             return(1);
  167.  
  168.         if (match(++s1, ++s2))
  169.             return(1);
  170.     }
  171.  
  172.     return(0);
  173. }
  174.